﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Macad.Interaction;
using Macad.Interaction.Dialogs;

namespace SpCad
{
    public   class sFile
    {
        #region 按钮是否可用
        public   bool IsNewFile() => InteractiveContext.Current?.DocumentController is null;
        public   bool IsSaveFile()
        {
            if (!(InteractiveContext.Current?.DocumentController is null)  && !(InteractiveContext.Current?.Document is null))
            {
                return InteractiveContext.Current.Document.HasUnsavedChanges;
            }
            return false;
        }
        public   bool IsSaveAll()=> ((InteractiveContext.Current?.Document != null) && InteractiveContext.Current.Document.HasUnsavedChanges);
        public   bool IsOpenModelFrom() => !(InteractiveContext.Current?.DocumentController is null);
        public   bool IsOpenFile(string fileName)=> !(InteractiveContext.Current?.DocumentController is null) && File.Exists(fileName);
        public   bool IsOpenRecentFile(string filename)=> InteractiveContext.Current?.DocumentController is not null;
        public   bool IsSaveFileAs()=> !(InteractiveContext.Current?.DocumentController is null) && !(InteractiveContext.Current?.Document is null);
        #endregion
        /// <summary>
        /// 新建文档
        /// </summary>
        public void NewFile()
        {
            if (InteractiveContext.Current.DocumentController.AskForSavingModelChanges())
                InteractiveContext.Current.DocumentController.NewModel();
        }
        /// <summary>
        /// 保存文件
        /// </summary>
        public   void SaveFile()
        {
            InteractiveContext.Current.DocumentController.SaveModel();
        }
        /// <summary>
        /// 另存为
        /// </summary>
        public   void SaveFileAs()
        {
            InteractiveContext.Current.DocumentController.SaveModelAs();
        }
        /// <summary>
        /// 保存所有
        /// </summary>
        public   void SaveAll()
        {
            if (InteractiveContext.Current.Document.HasUnsavedChanges)
            {
                InteractiveContext.Current.DocumentController.SaveModel();
            }
        }
        /// <summary>
        /// 打开文件
        /// </summary>
        public   void OpenFile()
        {
            if (InteractiveContext.Current.DocumentController.AskForSavingModelChanges())
                InteractiveContext.Current.DocumentController.OpenModelFrom(String.Empty);
        }
        /// <summary>
        /// 打开文件
        /// </summary>
        /// <param name="fileName"></param>
        public   void OpenFile(string fileName)
        {
            InteractiveContext.Current.DocumentController.OpenFile(fileName, false); 
        }
        /// <summary>
        /// 打开最近打开的文件
        /// </summary>
        /// <param name="fileName"></param>
        public   void OpenRecentFile(string fileName)
        {
            if (!File.Exists(fileName))
            {
                if (ErrorDialogs.RecentFileNotFound(fileName))
                    InteractiveContext.Current.DocumentController.RemoveFromMruList(fileName);
                return;
            }

            InteractiveContext.Current.DocumentController.OpenFile(fileName, false);
        }
    }
}
